 package com.op.crm.util;
 
 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
 public class DateUtil
 {
	
	 public static String datePattern = "MM/dd/yyyy";
	    public static String timePattern = "HH:mm";
	    
	    public static String datePatternCommon = "yyyy-MM-dd";
	    
	    public static String datePatternAll="yyyy-MM-dd HH:mm";
	    
	    public static String datePatternWithSecond="yyyy-MM-dd HH:mm:ss";
	    
	    public static String hourPattern ="HH";
	    
	    public static String minutePattern="mm";
	    
	    /** 格式化样式:年-月-日,年为4位 */
		public static final String YYYY_MM_DD = "yyyy-MM-dd";

		public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	 
   public static Date formatDateByString(String strDate)
   {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Date date = null;
     try {
       date = sdf.parse(strDate);
     } catch (ParseException e) {
      e.printStackTrace();
     }
    return date;
   }
 
   public static Date formatDateByString4(String strDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date date = null;
     try {
      date = sdf.parse(strDate);
     } catch (ParseException e) {
    e.printStackTrace();
     }
   return date;
   }
 
   public static Date formatDateByString2(String strDate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
     try {
       date = sdf.parse(strDate);
     } catch (ParseException e) {
       e.printStackTrace();
     }
     return date;
   }
 
   public static Date formatDateByString(String strDate, String format)
   {
     SimpleDateFormat sdf = new SimpleDateFormat(format);
     Date date = null;
     try {
      date = sdf.parse(strDate);
     } catch (ParseException e) {
       e.printStackTrace();
     }
    return date;
   }
 
   public static String formatStringByDate(Date date)
   {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String strDate = sdf.format(date);
    return strDate;
   }
 
   public static String formatStringByDate(Date date, String format)
   {
   SimpleDateFormat sdf = new SimpleDateFormat(format);
   String strDate = sdf.format(date);
   return strDate;
   }
 
   public static String formatStringByString(String date)
   {
  String newDate = "";
   Date d = formatDateByString(date);
    newDate = formatStringByDate(d, "yyyyMMdd");
    return newDate;
   }
 
   public static String formatStringByString(String date,String pattern)
   {
  String newDate = "";
   Date d = formatDateByString(date);
    newDate = formatStringByDate(d, pattern);
    return newDate;
   }
   
   public static long getDateLong(String date)
   {
    Date d = formatDateByString(date, "yyyy-MM-dd HH:mm");
     return d.getTime();
   }
 
   public static String getStringByLong(long date)
   {
    Date d = new Date(date);
    String sDate = formatStringByDate(d, "yyyy-MM-dd HH:mm:ss");
    return sDate;
   }
 
   public static Date getDateByDate(Date date)
   {
    String sDate = formatStringByDate(date, "yyyy-MM-dd HH:mm:ss");
   Date sf = formatDateByString(sDate);
   return sf;
   }
 
   public static String getDayweek(Date date) {  
       String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };  
       Calendar cal = Calendar.getInstance();  
       cal.setTime(date);  
       int dayinweek = cal.get(Calendar.DAY_OF_WEEK) - 1;  
       return weeks[dayinweek];  
   }  
   public static Date getWeekStart(Date date) {  
       Calendar cal = Calendar.getInstance();  
       cal.setTime(date);  
       int day = cal.get(Calendar.DAY_OF_WEEK) - 1;  
       cal.add(Calendar.DATE, -day + 1);  
       return cal.getTime();  
   }  
   public static Date getWeekEnd(Date date) {  
       Calendar cal = Calendar.getInstance();  
       cal.setTime(date);  
       int day = cal.get(Calendar.DAY_OF_WEEK) - 1;  
       cal.add(Calendar.DATE, 6 - day + 1);  
       return cal.getTime();  
   }   
   
   /**
	 * 将指定的日期字符串按照指定的样式格式化成相应的日期型数据
	 * 
	 * @param strDate
	 *            日期字符串
	 * @param strPattern
	 *            格式化样式
	 * @return 日期型数据实例或null[格式化错误]
	 */
	public static boolean validate(String strDate, String strPattern)
			throws Exception {

		if (strDate == null || "".equals(strDate)) {
			//throw new Exception("日期字符串为空：");
			return false;
		}

		if (strPattern == null || "".equals(strPattern)) {
			//throw new Exception("日期字符串的格式为空");
			return false;
		}

		if (YYYY_MM_DD.equals(strPattern)) {
			if (!strDate.matches("^\\d{4}\\-\\d{2}\\-\\d{2}$")) {
				//throw new Exception("日期字符串的格式与样式不符");
				return false;
			}
		} else if (YYYY_MM_DD_HH_MM.equals(strPattern)) {
			if (!strDate.matches("^\\d{4}\\-\\d{2}\\-\\d{2}\\s\\d{2}\\:\\d{2}$")) {
				//throw new Exception("日期字符串的格式与样式不符");
				return false;
			}
		}

		SimpleDateFormat format = new SimpleDateFormat(strPattern);
		format.setLenient(false);
		try {
			if (format.parse(strDate) != null)
				return true;
			else 
				return false;
		} catch (ParseException e) {
			//throw new Exception("日期字符串格式化失败");
			return false;
		}
	}
   
   public static void main(String[] args) {
	  
   }
 }